package morenoapp.components.common.table.column;

import javax.swing.JLabel;
import javax.swing.table.DefaultTableCellRenderer;
import morenoapp.components.common.table.renderer.NameTableCellRenderer;
import morenoapp.MorenoApp;
import morenoapp.components.common.table.renderer.RespondentProxyPanelTableCellRenderer;
import morenoapp.model.enums.MorenoSex;
import morenoapp.model.data.MorenoModelFace;
import morenoapp.model.data.MorenoRespondentFace;
import morenoapp.util.I18N;

/**
 *
 * @author dmitry.mamonov
 */
public class MorenoColumnsMeta {
    public static ColumnMeta createColumnMetaNumber() {
        return new AbstractColumnMeta("№", Integer.class, 35, //NOI18N
                new DefaultTableCellRenderer() {
                    {
                        setHorizontalAlignment(JLabel.TRAILING);
                    }
                }) {
            @Override
            public boolean isResizable() {
                return false;
            }

            @Override
            public int getMaximumWidth() {
                return getPrefferedWidth();
            }

            @Override
            public int getMinimumWidth() {
                return getPrefferedWidth();
            }

            @Override
            public Object getRowValue(int row) {
                return String.valueOf(row + 1);
            }
        };
    }

    public static ColumnMeta createColumnMetaName(final boolean paramEditable) {

        return new AbstractColumnMeta(I18N.get("label.last_name"),
                String.class,
                90,
                new RespondentProxyPanelTableCellRenderer(new NameTableCellRenderer())) {
            @Override
            public Object getRowValue(int row) {
                return getRespondentByIndex(row);
            }

            @Override
            public boolean isEditable() {
                return paramEditable;
            }

            @Override
            public void setRowValue(int row, Object value) {
                getRespondentByIndex(row).setName(value == null ? "" : value.toString());
            }
        };
    }

    public static ColumnMeta createColumnMetaMale() {
        return new AbstractColumnMeta(MorenoSex.MALE.toString(), String.class, 45) {
            @Override
            public int getMaximumWidth() {
                return getPrefferedWidth();
            }

            @Override
            public Object getRowValue(int row) {
                MorenoRespondentFace respondent = getRespondentByIndex(row);
                return respondent.getSex() == MorenoSex.MALE ? respondent.getSex().toString() : "";
            }
        };
    }

    public static ColumnMeta createColumnMetaFemale() {
        return new AbstractColumnMeta(MorenoSex.FEMALE.toString(), String.class, 45) {
            @Override
            public int getMaximumWidth() {
                return getPrefferedWidth();
            }

            @Override
            public Object getRowValue(int row) {
                MorenoRespondentFace respondent = getRespondentByIndex(row);
                return respondent.getSex() == MorenoSex.FEMALE ? respondent.getSex().toString() : "";
            }
        };
    }

    public static ColumnMeta createColumnMetaSex() {
        return new AbstractColumnMeta(I18N.get("label.sex"), String.class, 45) {
            @Override
            public int getMaximumWidth() {
                return getPrefferedWidth();
            }

            @Override
            public Object getRowValue(int row) {
                MorenoRespondentFace respondent = getRespondentByIndex(row);
                return respondent.getSex() != MorenoSex.UNKNOWN ? "<html>"+respondent.getSex().getHtmlDescription() : ""; //NOI18N
            }
        };
    }

    public static ColumnMeta createColumnMetaSexMale() {
        return new AbstractColumnMeta(I18N.get("lable.sex.male.short"), Boolean.class, 40) {
            @Override
            public int getMinimumWidth() {
                return getPrefferedWidth();
            }

            @Override
            public int getMaximumWidth() {
                return getPrefferedWidth();
            }

            @Override
            public Object getRowValue(int row) {
                MorenoRespondentFace respondent = getRespondentByIndex(row);
                return respondent.getSex() == MorenoSex.MALE;
            }

            @Override
            public void setRowValue(int row, Object value) {
                MorenoRespondentFace respondent = getRespondentByIndex(row);
                respondent.setSex(Boolean.TRUE.equals(value) ? MorenoSex.MALE : MorenoSex.UNKNOWN);
            }

            @Override
            public boolean isEditable() {
                return true;
            }
        };
    }

    public static ColumnMeta createColumnMetaSexFemale() {
        return new AbstractColumnMeta(I18N.get("label.sex.female.short"), Boolean.class, 40) {
            @Override
            public int getMinimumWidth() {
                return getPrefferedWidth();
            }

            @Override
            public int getMaximumWidth() {
                return getPrefferedWidth();
            }

            @Override
            public Object getRowValue(int row) {
                MorenoRespondentFace respondent = getRespondentByIndex(row);
                return respondent.getSex() == MorenoSex.FEMALE;
            }

            @Override
            public void setRowValue(int row, Object value) {
                MorenoRespondentFace respondent = getRespondentByIndex(row);
                respondent.setSex(Boolean.TRUE.equals(value) ? MorenoSex.FEMALE : MorenoSex.UNKNOWN);
            }

            @Override
            public boolean isEditable() {
                return true;
            }
        };
    }

    public static ColumnMeta createColumnMetaPassedPoll() {
        return new AbstractColumnMeta(I18N.get("label.passed_poll"), Boolean.class, 40) {
            @Override
            public int getMaximumWidth() {
                return 150;
            }

            @Override
            public Object getRowValue(int row) {
                return getRespondentByIndex(row).isPassedPoll();
            }

            @Override
            public boolean isEditable() {
                return true;
            }

            @Override
            public void setRowValue(int row, Object value) {
                getRespondentByIndex(row).setPassedPoll(Boolean.TRUE.equals(value));
            }
        };
    }

    public static MorenoRespondentFace getRespondentByIndex(int row) {
        return getMorenoModel().getRespondentsList().get(row);
    }

    private static MorenoModelFace getMorenoModel() {
        return MorenoApp.getApplicationData().getMorenoModelProxy();
    }
}
